import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class NoBorderTextField extends StatefulWidget {
  final String? initValue;
  final String? hintText;
  final Widget? prefixIcon;
  final Widget? sufferIcon;
  final TextInputType? keyboardType;
  final TextAlign? textAlign;
  final void Function(String v)? callback;
  final bool? showUnderLine;
  final VoidCallback? onTap;
  final bool? readOnly;
  final EdgeInsetsGeometry? contentPadding;
  final TextEditingController? controller;
  final TextStyle? style;
  final bool? filled;
  final Color? filledColor;
  final Color? borderColor;
  final bool? autoFocus;
  final FocusNode? focusNode;
  final int? maxLength;
  final int? maxLines;
  final bool? obscureText;
  final TextStyle? hintStyle;
  final List<TextInputFormatter>? inputFormatters;
  final String counterText;
  final bool? isDense;

  const NoBorderTextField({
    super.key,
    required this.hintText,
    this.initValue = "",
    this.prefixIcon,
    this.sufferIcon,
    this.keyboardType = TextInputType.text,
    this.textAlign = TextAlign.start,
    this.callback,
    this.showUnderLine = true,
    this.onTap,
    this.readOnly = false,
    this.contentPadding = const EdgeInsets.only(),
    this.controller,
    this.style,
    this.filled = false,
    this.filledColor,
    this.borderColor,
    this.autoFocus,
    this.focusNode,
    this.maxLength,
    this.maxLines,
    this.obscureText,
    this.hintStyle,
    this.inputFormatters,
    this.counterText = "",
    this.isDense,
  });

  @override
  State<NoBorderTextField> createState() => _NoBorderTextFieldState();
}

class _NoBorderTextFieldState extends State<NoBorderTextField> {
  TextEditingController? _controller;

  @override
  void initState() {
    super.initState();
    _controller =
        widget.controller ?? TextEditingController(text: widget.initValue)
          ..addListener(() {
            debugPrint("输入框的数据::::${_controller!.text}");
            if (widget.callback != null) {
              widget.callback!(_controller!.text);
            }
          });
  }

  // @override
  // void didUpdateWidget(covariant NoBorderTextField oldWidget) {
  //   super.didUpdateWidget(oldWidget);
  //   _controller = widget.controller ?? TextEditingController(text: widget.initValue)..addListener(() {
  //     // print("输入框的数据::::${_controller.text}");
  //     if(widget.callback != null){
  //       widget.callback(_controller.text);
  //     }
  //   });
  // }

  @override
  void dispose() {
    super.dispose();
    _controller?.removeListener(() {});
    _controller?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: TextField(
        focusNode: widget.focusNode,
        autofocus: widget.autoFocus ?? false,
        // cursorHeight: ,
        readOnly: widget.readOnly!,
        controller: _controller,
        textAlign: widget.textAlign!,
        maxLength: widget.maxLength ?? 99,
        maxLines: widget.maxLines ?? 1,
        style: widget.style ?? TextStyle(color: Colors.black, fontSize: 16.sp),
        decoration: InputDecoration(
          isDense: widget.isDense ?? true,
          counterText: widget.counterText,
          filled: widget.filled,
          fillColor: widget.filledColor,
          hintText: widget.hintText,
          hintStyle: widget.hintStyle ??
              TextStyle(fontSize: 16.sp, color: const Color(0xff999999)),
          prefixIcon: widget.prefixIcon,
          suffixIcon: widget.sufferIcon,
          prefixIconConstraints: const BoxConstraints(),
          suffixIconConstraints: const BoxConstraints(),
          contentPadding: widget.contentPadding,
          border: InputBorder.none,
          // enabledBorder: widget.showUnderLine! ? UnderlineInputBorder(borderSide: BorderSide(color: widget.borderColor ?? Style.boardColor)) : null,
          // focusedBorder: widget.showUnderLine! ? UnderlineInputBorder(borderSide: BorderSide(color: widget.borderColor ?? Style.boardColor)) : null,
        ),
        keyboardType: widget.keyboardType,
        onTap: widget.onTap,
        obscureText: widget.obscureText ?? false,
        inputFormatters: widget.inputFormatters ?? <TextInputFormatter>[],
      ),
    );
  }
}
